//
//  AnswerListViewController.swift
//  YLH
//
//  Created by qinjiangtao on 2024/1/15.
//  答题列表

import UIKit
import YYText

class AnswerListViewController: QIBaseViewController {
    
    /// 问题ID
    var problemID = ""
    var typeStr = ""
    
    private var dataArr = [HappinessDetailListModel]()

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "\(typeStr)分析结果"

        
        self.view.addGradientColor(fromColors: [ColorWithStr("#FFB2B5").cgColor, ColorWithStr("#FCDFE0").cgColor, ColorWithStr("#FFFBFB").cgColor], locationArr: [0, 0.5, 1.0], with: .topToBottom)
        view.addSubview(tableView)
        
        getData()
    }
    
    private func getData() {
        var url = ""
        if typeStr == "幸福感" {
            url = "/Project/happyDetail"
        } else {
            url = "/Project/lifeDetail"
        }
        QINetworkManager.requestJson(urlStr: url, method: .get, parameters: ["id": problemID]) { json in
            print(json)
            self.dataArr = json.arrayObject?.kj.modelArray(HappinessDetailListModel.self) ?? [HappinessDetailListModel]()
            self.tableView.reloadData()
        }
    }
    

    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRect.zero, style: .grouped)
        tableView.frame = CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - NAV_HEIGHT )
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0
        } else {}
        return tableView
    }()

}

extension AnswerListViewController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArr.count
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        1
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        UIView()
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let model = dataArr[indexPath.row]
        let cell = UITableViewCell()
        cell.backgroundColor = .clear
        cell.selectionStyle = .none
        cell.textLabel?.font = SYSFONT(ofSize: 14)
        cell.textLabel?.text = "\(model.parent.name):  \(model.name) "
        cell.textLabel?.numberOfLines = 0
        return cell
    }
    
}

//extension AnswerListViewController: UITableViewDataSource, UITableViewDelegate {
//    
//    func numberOfSections(in tableView: UITableView) -> Int {
//        dataArr.count
//    }
//    
//    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
//        let model = dataArr[section]
//        let size = model.name.tt_size(with: SYSFONTBOLD(ofSize: 14), constrainedToWidth: SCREEN_WIDTH - 40)
//        return size.height + 10
//    }
//    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//        let headerView = UIView()
//        let model = dataArr[section]
//        
//        let size = model.name.tt_size(with: SYSFONTBOLD(ofSize: 14), constrainedToWidth: SCREEN_WIDTH - 40)
//        let titleL = UILabel().then({
//            $0.frame = CGRect(x: 20, y: 5, width: size.width, height: size.height)
//            $0.text = model.name
//            $0.numberOfLines = 0
//            $0.textColor = MainColor
//            $0.font = SYSFONTBOLD(ofSize: 14)
//        })
//        headerView.addSubview(titleL)
//        return headerView
//    }
//    
//    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
//        20
//    }
//    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
//        UIView()
//    }
//    
//    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        let model = dataArr[section]
//        return model.children.count
//    }
//    
//    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        30
//    }
//    
//    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell = AnswerListViewCell11()
//        cell.selectionStyle = .none
//        cell.backgroundColor = .clear
//        if selectIndexArr[indexPath.section] == indexPath.row {
//            cell.isSelect = true
//        }
//        let model = dataArr[indexPath.section]
//        cell.model = model.children[indexPath.row]
//        return cell
//    }
//    
//    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        selectIndexArr[indexPath.section] = indexPath.row
//        print(selectIndexArr)
//        tableView.reloadData()
//        
//        let model = dataArr[indexPath.section]
//        let childModel = model.children[indexPath.row]
//        answerArr[indexPath.section] = childModel.id
//    }
//    
//}


class AnswerListViewCell: UITableViewCell {
    
    private var selectImgView = UIImageView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        addUI()
    }
    func addUI() {
        
        selectImgView = UIImageView().then {
            $0.frame = CGRect(x: 20, y: 0, width: 14, height: 14)
            $0.centerY = self.centerY
            $0.image = UIImage(named: "题_未选择")
        }
        self.addSubview(selectImgView)
        
        let contentL = UILabel().then({
            $0.frame = CGRect(x: selectImgView.right + 5, y: 0, width: SCREEN_WIDTH - 50, height: self.height)
            $0.text = "内容内容内容内容内容"
            $0.textColor = Color666666
            $0.font = SYSFONT(ofSize: 13)
        })
        self.addSubview(contentL)
        
    }
    
    var isSelect = false {
        didSet {
            if isSelect {
                selectImgView.image = UIImage(named: "题_选择")
            } else {
                selectImgView.image = UIImage(named: "题_未选择")
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}

